#!/bin/bash

echo "删除旧的文件包 ..."

rm -rf $GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-gateway/
rm -rf $GOPATH/src/google/
rm -rf $GOPATH/pkg/mod/google.golang.org
rm -rf $GOPATH/pkg/mod/github.com/grpc-ecosystem/
rm -rf $GOPATH/pkg/mod/golang.org/x/
rm -rf $GOPATH/src/github.com/grpc-ecosystem/

rm -rf $GOPATH/bin/go-bindata
rm -rf $GOPATH/bin/protoc
rm -rf $GOPATH/bin/protoc-gen-go
rm -rf $GOPATH/bin/protoc-gen-go-grpc
rm -rf $GOPATH/bin/protoc-gen-grpc-gateway
rm -rf $GOPATH/bin/protoc-gen-openapiv2
rm -rf $GOPATH/bin/protoc-gen-swagger

echo "finished"

##
echo "1.获取grpc 基础依赖 ..."
go get -u google.golang.org/grpc
go get -u github.com/golang/protobuf
go get -u github.com/golang/protobuf/protoc-gen-go
echo "finished"


echo "2.install protoc ..."
PROTOC_ZIP=protoc-3.14.0-linux-x86_64.zip
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.14.0/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d $GOPATH bin/protoc
rm -f $PROTOC_ZIP
export PATH=$PATH:$GOPATH/bin
echo "finished"


echo "3.downloading grpc-gateway release package v1.16.0 ..."
rm -rf $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway
mkdir -p $GOPATH/src/github.com/grpc-ecosystem
cd $GOPATH/src/github.com/grpc-ecosystem
wget https://github.com/grpc-ecosystem/grpc-gateway/archive/v1.16.0.zip -O grpc-gateway.zip
unzip grpc-gateway.zip
rm -f grpc-gateway.zip
mv grpc-gateway-1.16.0 grpc-gateway
echo "finished"

echo "4. building grpc gateway ..."
cd $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go build
go install
echo "finished"

echo "5. building swagger ..."
cd $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go build
go install
echo "finished"


echo "6. mv google/api/annonation files ..."
cp -r $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/ $GOPATH/src/
go build
go install
echo "finished"


echo "7. downloading protobuf files "
cd ~
wget https://github.com/protocolbuffers/protobuf/archive/v3.14.0.zip -O annonations.zip
unzip annonations.zip
rm -f annonations.zip
cp -r protobuf-3.14.0/src/google/* $GOPATH/src/google/
rm -rf protobuf-3.14.0
echo "finished"


echo "8. install go-bindata and go-bindata-assetfs ..."
go get -u github.com/jteeuwen/go-bindata/...
export PATH=$PATH:$GOPATH/bin
go get -u github.com/elazarl/go-bindata-assetfs
echo "finished"

echo "9. copy protoc-gen-swagger to GOPATH/src ..."
rm -rf $GOPATH/src/protoc-gen-swagger/
cp -r $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/ $GOPATH/src/
echo "finished"


echo "10. get swagger-ui and go-bindata ..."
wget https://github.com/swagger-api/swagger-ui/archive/refs/tags/v4.13.0.tar.gz
tar -xvf v4.13.0.tar.gz
mkdir -p $GOPATH/src/swagger-ui
mv swagger-ui-4.13.0/dist/ $GOPATH/src/swagger-ui/
rm -rf swagger-ui-4.13.0
rm -rf v4.13.0.tar.gz




### another source way
# wget https://raw.githubusercontent.com/zq2599/blog_download_files/master/files/swagger-ui.zip -O swagger-ui.zip
# unzip swagger-ui-4.13.0.zip



## 这个命令根据项目需求执行，-o 是生成datafile.go的路径，最后的参数是要转换的源码位置，必须能被项目找到
# cd myproject
# cp $GOPATH/src/swagger-ui/ ./
# go-bindata --nocompress -pkg swagger -o ./swagger-ui/datafile.go /swagger-ui/dist/...


## 这个命令需要手动执行，用来编译proto文件
# protoc -I. \
# -I$GOPATH/src \
# --go_out=plugins=grpc:protos \
# --grpc-gateway_out=logtostderr=true:protos \
# --swagger_out=logtostderr=true:pb \
# protos/*.proto



